package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.SignUpList;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.SignView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/28 0028.
 * 说明：
 */

public class SignPersenter extends BasePresent<SignView> {

    RetrofitSerives retrofitSerives;
    Call<SignUpList> signUpListCall;

    public void getSignUpList(Integer activityId) {

        view.showProgress();
        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("activity_id", activityId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        signUpListCall = retrofitSerives.getSignUpList(body);
        signUpListCall.enqueue(new Callback<SignUpList>() {
            @Override
            public void onResponse(Call<SignUpList> call, Response<SignUpList> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {

                        view.setSignUpList(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<SignUpList> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
